package com.web.cms.doc.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.web.cms.doc.model.Channel;
import com.web.cms.doc.message.ChannelRequest;
import com.web.cms.doc.message.ChannelResponse;

public class ChannelConvertor
{
	private ChannelConvertor()
	{
	}
	
	public static Channel toChannel(ChannelRequest request)
	{
		if (request == null)
			return null;
		
		Channel channel = new Channel();
		channel.setId(request.getId());
		channel.setParentId(request.getParentId());
		channel.setWebId(request.getWebId());
		channel.setCode(request.getCode());
		channel.setName(request.getName());
		channel.setDescp(request.getDescp());
		channel.setDocs(request.getDocs());
		channel.setType(request.getType());
		channel.setModel(request.getModel());
		channel.setSort(request.getSort());
		channel.setState(request.getState());
		return channel;
	}
	
	public static ChannelResponse toResponse(Channel channel)
	{
		if (channel == null)
			return null;
		
		ChannelResponse response = new ChannelResponse();
		response.setId(channel.getId());
		response.setParentId(channel.getParentId());
		response.setWebId(channel.getWebId());
		response.setCode(channel.getCode());
		response.setName(channel.getName());
		response.setDescp(channel.getDescp());
		response.setDocs(channel.getDocs());
		response.setType(channel.getType());
		response.setModel(channel.getModel());
		response.setSort(channel.getSort());
		response.setState(channel.getState());
		return response;
	}
	
	public static List<ChannelResponse> toResponse(List<Channel> list)
	{
		List<ChannelResponse> responseList = new ArrayList<>();
		if (list == null || list.isEmpty())
			return responseList;
		
		for (Channel channel : list)
		{
			responseList.add(toResponse(channel));
		}
		return responseList;
	}
	
	public static Map<Integer, List<ChannelResponse>> toResponse(Map<Integer, List<Channel>> map)
	{
		Map<Integer, List<ChannelResponse>> mapResponse = new HashMap<>();
		if (map == null || map.isEmpty())
			return mapResponse;
		
		for (Map.Entry<Integer, List<Channel>> entry : map.entrySet())
		{
			mapResponse.put(entry.getKey(), toResponse(entry.getValue()));
		}
		return mapResponse;
	}
	
	public static Map<String, List<ChannelResponse>> toTree(Map<Integer, List<Channel>> map)
	{
		Map<String, List<ChannelResponse>> mapResponse = new HashMap<>();
		if (map == null || map.isEmpty())
			return mapResponse;
		
		for (Map.Entry<Integer, List<Channel>> entry : map.entrySet())
		{
			mapResponse.put(String.valueOf(entry.getKey()), toResponse(entry.getValue()));
		}
		return mapResponse;
	}
	
}
